package com.qf.mvc2202.controller;

import com.qf.mvc2202.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/pass")
public class PassVauleController {

    @RequestMapping("/test1")
    public String test1(HttpServletRequest req , HttpSession session){

        // 调用 UserServic 查user
        final User user = new User();
        user.setId(200);
        user.setName("luffy");

        // 把查到的数据放入对应的作用域对象中
        session.setAttribute("user",user);

        req.setAttribute("age",19);

        // 选择视图进行响应
        return "forward:/view/user.jsp";

    }



    //model中的数据，会在V渲染之前，将数据复制一份给request
    @RequestMapping("/test2")
    public String testData(Model model){
        model.addAttribute("age", "25");
        // 默认就是 forward
        return "/view/user.jsp";
    }


    @RequestMapping("/test3")
    public ModelAndView test3(ModelAndView modelAndView){
        // 设置要转发的视图
        modelAndView.setViewName("/view/user.jsp");
        // 设置 model层的返回的数据
        modelAndView.addObject("age",26);

        return modelAndView;
    }

}
